# Copyright (c) 2025 Li Auto Inc. and its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# executable_internal.mk
#

include $(BUILD_SYSTEM)/base_rules.mk

ifeq ($(LOCAL_LINK_SCRIPT),)
    ifneq ($(DEFAULT_LDS),)
    LOCAL_LINK_SCRIPT:=$(DEFAULT_LDS)
    endif
else
    LOCAL_LINK_SCRIPT:=$(LOCAL_PATH)/$(LOCAL_LINK_SCRIPT)
endif

# FIXME
# It's really correct that use the following method.
#     PRIVATE_LIBRARY:=$(foreach lib,$(LOCAL_STATIC_LIBRARIES), $(call get_module_target,$(lib)))
# However, there is multiple targets in $(ALL_MODULES.$(lib).BUILT), such as MZCU prebuilt targets.
# In this case, these multiple targets couldn't be a prebuilt target.
$(LOCAL_TARGET): PRIVATE_ALL_OBJS:=$(LOCAL_C_OBJS) $(LOCAL_CPP_OBJS) $(LOCAL_EXTRA_OBJS)
$(LOCAL_TARGET): PRIVATE_LIBRARY:=$(sort $(call module-built-files, $(ALL_MODULES.$(LOCAL_MODULE).STATIC_LIB)))
$(LOCAL_TARGET): PRIVATE_IMPORT_INCLUDES:= $(import_includes)
$(LOCAL_TARGET): PRIVATE_CFLAGS:= $(GLOBAL_CFLAGS) $(LOCAL_CFLAGS)
$(LOCAL_TARGET): PRIVATE_LFLAGS:= $(GLOBAL_LFLAGS) $(LOCAL_LFLAGS)
$(LOCAL_TARGET): PRIVATE_LDS:= $(LOCAL_LINK_SCRIPT)
$(LOCAL_TARGET): PRIVATE_INCLUDES:=$(addprefix $(LOCAL_PATH)/, $(LOCAL_INC_DIR)) $(LOCAL_EXTRA_INCS)

$(LOCAL_TARGET): $(foreach lib,$(LOCAL_STATIC_LIBRARIES), $(ALL_MODULES.$(lib).BUILT))
$(LOCAL_TARGET): $(LOCAL_C_OBJS) $(LOCAL_CPP_OBJS) $(LOCAL_EXTRA_OBJS)
$(LOCAL_TARGET): $(LOCAL_MODULE_MAKEFILE)

$(LOCAL_TARGET):
	$(transform-o-to-executable)

$(LOCAL_TARGET).tp: PRIVATE_TEST_PROFILES:=$(LOCAL_TEST_PROFILES)
$(LOCAL_TARGET).tp: $(LOCAL_TEST_PROFILES) $(LOCAL_MODULE_MAKEFILE)
	@echo "GEN	$@"
	$(hide)mkdir -p $(dir $@)
ifneq ($(LOCAL_TEST_PROFILES),)
	$(hide)for tp in $(PRIVATE_TEST_PROFILES);		\
               do echo "" >> $@; cat  $${tp} >> $@; done
else
	$(hide)touch $@
endif

$(LOCAL_TARGET).robot: $(LOCAL_TARGET).tp
	$(call transform-tp-to-robot, $(TARGET_PRODUCT))

$(LOCAL_C_OBJS): $(LOCAL_OUT)/%.o: $(LOCAL_PATH)/%.c
	$(transform-c-to-o)
	$(transform-c-to-cmd)

ifneq ($(LOCAL_EXTRA_OBJS),)
$(LOCAL_EXTRA_OBJS): %.o: %.c
	$(transform-c-to-o)
	$(transform-c-to-cmd)
endif

$(LOCAL_CPP_OBJS): $(LOCAL_OUT)/%.o: $(LOCAL_PATH)/%.cpp
	$(transform-cpp-to-o)
	$(transform-cpp-to-cmd)

# Provide some fake target for using easily
$(LOCAL_MODULE): $(LOCAL_TARGET)

$(LOCAL_MODULE)-robot: $(LOCAL_TARGET).robot

$(LOCAL_MODULE)-install: $(LOCAL_TARGET) $(LOCAL_TARGET).robot
	$(call install-executable,$<,$<.robot)

.PHONY: $(LOCAL_MODULE) $(LOCAL_MODULE)-robot $(LOCAL_MODULE)-install
# vim:ft=make
#
